{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6aa404db-55e4-4f19-94e0-d4f4b7406b85",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/autodl-tmp/yolo_incremental_learning/utils/general.py:32: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.\n",
      "  import pkg_resources as pkg\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import cv2\n",
    "from MyTools import kitti_2_yolo\n",
    "import random\n",
    "import shutil\n",
    "import sys\n",
    "import xml.etree.ElementTree as ET\n",
    "from tqdm import tqdm\n",
    "from utils.general import download, Path, os\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "68b5f83b-5907-4aa5-9181-b070be177c6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def cp_2_target(img_path, label_path, img_save_folder, label_save_folder, img_name):\n",
    "    label_name = os.path.splitext(img_name)[0] + \".txt\"\n",
    "    train_img_path = os.path.join(img_path, img_name)\n",
    "    train_label_path = os.path.join(label_path, label_name)\n",
    "    \n",
    "    if not os.path.exists(train_img_path) or not os.path.exists(train_label_path):\n",
    "        sys.exit()\n",
    "\n",
    "    shutil.copy(train_img_path, img_save_folder)\n",
    "    shutil.copy(train_label_path, label_save_folder)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4505b0bc-1338-48e2-9190-040e0566b809",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.makedirs('/root/autodl-tmp/datasets/VOC/images/kitti_old')\n",
    "os.makedirs('/root/autodl-tmp/datasets/VOC/labels/kitti_old')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b8dd2032-ef07-4706-bcda-a2678e42966d",
   "metadata": {},
   "outputs": [],
   "source": [
    "img_path = '/root/autodl-tmp/datasets/kitti/images/train'\n",
    "label_path = '/root/autodl-tmp/datasets/kitti/labels/train'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8e499db3-b9ab-4d18-969e-18cda4560375",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_imgs = os.listdir(img_path) # 得到所有文件的名字\n",
    "random.shuffle(all_imgs)\n",
    "img_num = len(all_imgs)\n",
    "train_num = int(img_num / 2)\n",
    "train_imgs = all_imgs[ : train_num]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ec8433cf-777a-4ade-acb8-11d817ff9cef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已完成 0% (0/2094)\n",
      "已完成 10% (209/2094)\n",
      "已完成 20% (418/2094)\n",
      "已完成 30% (627/2094)\n",
      "已完成 40% (836/2094)\n",
      "已完成 50% (1045/2094)\n",
      "已完成 60% (1254/2094)\n",
      "已完成 70% (1463/2094)\n",
      "已完成 80% (1672/2094)\n",
      "已完成 90% (1881/2094)\n",
      "已完成 100% (2090/2094)\n"
     ]
    }
   ],
   "source": [
    "for i, train_img in enumerate(train_imgs):\n",
    "    if train_img.endswith(('.jpg', '.png', '.jpeg')):\n",
    "        cp_2_target(img_path, \n",
    "                    label_path, \n",
    "                    '/root/autodl-tmp/datasets/VOC/images/kitti_old', \n",
    "                    '/root/autodl-tmp/datasets/VOC/labels/kitti_old',  \n",
    "                    train_img) #得到一个图片和label的路径\n",
    "    if i % (train_num // 10) == 0:\n",
    "        progress = (i / train_num) * 100\n",
    "        print(f\"已完成 {progress:.0f}% ({i}/{train_num})\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfec97d9-5e5a-4e12-9537-eac2c1c0c1ce",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
